package com.lin.controller;

import com.lin.VO.ProductInfoVO;
import com.lin.VO.ProductVO;
import com.lin.VO.ResultVO;
import com.lin.dataobject.ProductCategory;
import com.lin.dataobject.ProductInfo;
import com.lin.server.ProductCategoryService;
import com.lin.server.ProductInfoService;
import com.lin.utils.ResultVOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by MYQ on 2017/8/29.
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;
    @GetMapping("/list")
    public ResultVO list(){
        List<ProductInfo> productInfoList= productInfoService.findUpAll();

        List<Integer> categoryTypeList=new ArrayList<>();

        categoryTypeList=productInfoList.stream()
                .map(e->e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList= productCategoryService.findByCategoryTypeIn(categoryTypeList);

        List<ProductVO> productVOList=new ArrayList<>();
        for (ProductCategory productCategory:productCategoryList) {
            ProductVO productVO=new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVO> productInfoVOList=new ArrayList<>();
            for (ProductInfo productInfo:productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO=new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }

            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }


        return ResultVOUtils.success(productVOList);
    }
}
